package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.mbg.model.PmsBrand;
import com.macro.mall.tiny.service.PmsBrandService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "PmsBrandController", description = "商品品牌管理")
@Controller
@RequestMapping("/brand")
public class PmsBrandController {
    //如何使用日志来监控类
    private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);

    @Autowired
    private PmsBrandService pmsBrandService;
    @ApiOperation("获取所有品牌列表")
    @ResponseBody
    @RequestMapping(value = "/listAll" , method = RequestMethod.GET)
    public CommonResult<List<PmsBrand>> getBrandList(){
        List<PmsBrand> list = pmsBrandService.listAllBrand();
        return CommonResult.success(list);
    }

    //@RequestBody他会接收前台发送的json格式的参数{"name":"tom","age":"20"}并转换为对象
    //@ResponseBody从后台向前台返回json格式的数据
    @ApiOperation("添加品牌")
    @RequestMapping(value = "/create" , method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addBrand(@RequestBody PmsBrand pmsBrand){
        CommonResult commonResult;
        int a = pmsBrandService.creatBrand(pmsBrand);
        if(a==1){
            commonResult = CommonResult.success(pmsBrand);
            LOGGER.debug("createBrand success",pmsBrand);
        }else {
            commonResult = CommonResult.failed("添加失败");
            LOGGER.debug("createBrand failed",pmsBrand);
        }
        return commonResult;
    }
    @ApiOperation("分页查询品牌列表")
    @RequestMapping(value = "/list" , method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsBrand>> listBrandByPage(@RequestParam(value = "pageNum",defaultValue = "1")
                                                              @ApiParam("页码") Integer pageNum,
                                                              @RequestParam(value = "pageSize",defaultValue = "3")
                                                              @ApiParam("每页数量") Integer pageSize){
        List<PmsBrand> list = pmsBrandService.listBrand(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }
    @ApiOperation("获取指定id的品牌")
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> brand(@PathVariable("id") Long id){
        return CommonResult.success(pmsBrandService.getBrand(id));
    }
    @ApiOperation("删除指定id的品牌")
    @RequestMapping(value = "/delete/{id}" , method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteBrand(@PathVariable("id") Long id){
        int a = pmsBrandService.deleteBrand(id);
        if (a==1){
            LOGGER.debug("deleteBrand success :id={}",id);
            return CommonResult.success(null);
        }else {
            LOGGER.debug("deleteBrand failed :id={}",id);
            return CommonResult.failed("操作失败");
        }
    }
    @ApiOperation("修改指定id的品牌信息")
    @RequestMapping(value = "/update/{id}" , method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id") Long id ,@RequestBody PmsBrand pmsBrand){
        CommonResult commonResult;
        int a = pmsBrandService.updateBrand(id,pmsBrand);
        if(a==1){
            commonResult = CommonResult.success(pmsBrand);
            LOGGER.debug("updateBrand success",pmsBrand);
        }else {
            commonResult = CommonResult.failed("修改失败");
            LOGGER.debug("updateBrand failed",pmsBrand);
        }
        return commonResult;
    }

}
